//
//  NavSliderController.m
//  BAlliance
//
//  Created by coding Tools on 16/7/20.
//  Copyright © 2016年 jack. All rights reserved.
//

#import "NavSliderController.h"

@interface NavSliderController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property(nonatomic, strong) UIView *customTitle;
@property(nonatomic, strong) UIView *lineView;
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) UIButton *selectedButton;
@property(nonatomic, strong) NSArray *controllers;
@property(nonatomic, strong) NSArray *titles;
/// nomalColor:(UIColot) selectedColor:(UIColot) titleFont:(CGFloat) titlesMargin:(CGFloat) titleWithLineMargin:(CGFloat) lineHeight:(CGFloat)
@property(nonatomic, strong) NSDictionary *titleAttributes;

@property(nonatomic, strong) UIView *titleView;



@end



NSString *KNavSliderCollectionViewCell = @"KNavSliderCollectionViewCellIdentity";

@implementation NavSliderController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
}
- (void)prepareForNavSliderWithTitles:(NSArray *)titles controllers:(NSArray *)controllers titleAttr:(NSDictionary *)titleAttributes
{
    self.titleAttributes = titleAttributes;
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:KNavSliderCollectionViewCell];
    self.navigationItem.titleView = [self creatNavgationItemWithTitles:titles];
    self.controllers = controllers;
    self.titles = titles;
    [self selectedTitleButtonWithIndex:0];
}
- (void)selectedItemForCollectionViewWithIndex:(NSInteger)index
{
    [self selectedTitleButtonWithIndex:index];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
}
- (void)setNavgationItemUserInteractionEnabled:(BOOL)enable
{
    self.navigationItem.titleView.userInteractionEnabled = enable;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KNavSliderCollectionViewCell forIndexPath:indexPath];
    if (self.controllers != nil && self.controllers.count > 0) {
        UIViewController *viewController = self.controllers[indexPath.item];
        viewController.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
        [cell addSubview:viewController.view];
    }
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self selectedTitleButtonWithIndex:index];
    self.currentIndex = index;
}
- (void)selectedTitleButtonWithIndex:(NSInteger)index
{
    for (UIView *view in self.customTitle.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *btn = (UIButton *)view;
            if (btn.tag == index) {
                btn.selected = YES;
                [self changeFrameForLineView:btn];
            } else {
                btn.selected = NO;
            }
        }
    }
}

- (void)titleButtonClick:(UIButton *)sender
{
    self.selectedButton.selected = NO;
    sender.selected = YES;
    self.selectedButton = sender;
    [self changeFrameForLineView:sender];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:sender.tag inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    self.currentIndex = sender.tag;
}
- (void)changeFrameForLineView:(UIButton *)sender
{
    CGRect senderR = sender.frame;
    [UIView animateWithDuration:0.25 animations:^{
        self.lineView.frame = CGRectMake(senderR.origin.x, senderR.size.height + [self.titleAttributes[@"titleWithLineMargin"] floatValue], senderR.size.width, [self.titleAttributes[@"lineHeight"] floatValue]);
    }];
}
- (UIView *)creatNavgationItemWithTitles:(NSArray *)titles
{
    UIView *view = [[UIView alloc] init];
    CGFloat lastX = 0;
    for (int i = 0; i < titles.count; i++) {
        UIButton *button = [[UIButton alloc] init];
        button.tag = i;
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitleColor:self.titleAttributes[@"nomalColor"] forState:UIControlStateNormal];
        [button setTitleColor:self.titleAttributes[@"selectedColor"] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:[self.titleAttributes[@"titleFont"] floatValue]];
        NSString *title = titles[i];
        CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:[self.titleAttributes[@"titleFont"] floatValue]]}];
        [button setFrame:CGRectMake(lastX, 0, titleSize.width, titleSize.height)];
        [button addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        if (i == titles.count - 1) {
            lastX += titleSize.width;
        } else {
            lastX += (titleSize.width + [self.titleAttributes[@"titlesMargin"] floatValue]);
        }
    }
    
    NSString *text = @"正";
    CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:[self.titleAttributes[@"titleFont"] floatValue]]}];
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor whiteColor];
    view.frame = CGRectMake(0, 0, lastX, textSize.height + [self.titleAttributes[@"titleWithLineMargin"] floatValue] + [self.titleAttributes[@"lineHeight"] floatValue] + 1);
    lineView.frame = CGRectMake(0, textSize.height + [self.titleAttributes[@"titleWithLineMargin"] floatValue], 0, [self.titleAttributes[@"lineHeight"] floatValue]);
    [view addSubview:lineView];
    self.customTitle = view;
    self.lineView = lineView;
    return view;
}

- (UICollectionView *)collectionView
{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:flowLayout];
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.pagingEnabled = YES;
    }
    return _collectionView;
}

- (void)setTitleAttributes:(NSDictionary *)titleAttributes
{
    /// nomalColor:(UIColot) selectedColor:(UIColot) titleFont:(CGFloat) titlesMargin:(CGFloat)
    if (titleAttributes[@"nomalColor"] == nil) {
        [titleAttributes setValue:[UIColor whiteColor] forKey:@"nomalColor"];
    }
    if (titleAttributes[@"selectedColor"] == nil) {
        [titleAttributes setValue:RGB(160, 160, 160) forKey:@"selectedColor"];
    }
    if (titleAttributes[@"titleFont"] == 0 || titleAttributes[@"titleFont"] == nil) {
        [titleAttributes setValue:@(14) forKey:@"nomalColor"];
    }
    if (titleAttributes[@"titlesMargin"] == 0 || titleAttributes[@"titlesMargin"] == nil) {
        [titleAttributes setValue:@(10) forKey:@"titlesMargin"];
    }
    _titleAttributes = titleAttributes;
}
- (void)setControllers:(NSArray *)controllers
{
    _controllers = controllers;
    for (UIViewController *controller in controllers) {
        [self addChildViewController:controller];
    }
}


@end
